<?php
/**
 * Each engineer has a duty to keep the code elegant
 * Created by wolffy.
 * Author @wolffy<13750088090@sina.cn>
 * Date: 2018/8/12
 * Time: 11:40
 */
namespace App\Repositories;

use App\Models\CardLogo;
use App\Models\CreditCardHandle;
use App\Models\CreditCardHandleGrant;

class CreditCardHandleRepository
{
    public function getList($page, $limit)
    {
        $page -= 1;
        $page = $page < 0 ? 0 : $page;
        $offset = $page * $limit;
        $list = CreditCardHandle::where('is_open', 1)
            ->offset($offset)
            ->limit($limit)
            ->orderBy('sort', 'asc')
            ->orderBy('id', 'asc')
            ->select('id', 'profit_money', 'bank_code', 'title', 'keywords', 'card_url')
            ->get();
        $data = [];
        foreach ($list as $key=>$value) {
            $cardLogo = CardLogo::where('bank_code', $value['bank_code'])->first();
            $data[$key] = $value;
            $data[$key]['card_logo_url'] = !empty($cardLogo) ? getOssImageUrl($cardLogo->card_logo_url) : '';
            $data[$key]['card_url'] = !empty($value['card_url']) ? getOssImageUrl($value['card_url']) : '';
            unset($data[$key]['bank_code']);
        }
        return $data;
    }

    /**
     * 办卡进度查询
     * @param $page
     * @param $limit
     * @return array
     */
    public function selectHandleResult($page, $limit)
    {
        $page -= 1;
        $page = $page < 0 ? 0 : $page;
        $offset = $page * $limit;
        $list = CreditCardHandle::where('is_open', 1)
            ->offset($offset)
            ->limit($limit)
            ->orderBy('sort', 'asc')
            ->orderBy('id', 'asc')
            ->select('bank_code', 'title', 'keywords', 'query_url')
            ->get();
        $data = [];
        foreach ($list as $key=>$value) {
            $cardLogo = CardLogo::where('bank_code', $value['bank_code'])->first();
            $data[$key] = $value;
            $data[$key]['card_logo_url'] = !empty($cardLogo) ? getOssImageUrl($cardLogo->card_logo_url) : '';
            unset($data[$key]['bank_code']);
        }
        return $data;
    }

    /**
     * 办理规则
     * @return array
     */
    public function handleRules()
    {
        $list = CreditCardHandle::where('is_open', 1)
            ->orderBy('sort', 'asc')
            ->orderBy('id', 'asc')
            ->select('profit_money', 'bank_code', 'bank_description', 'grant_type_id')
            ->get();
        $data = [];
        foreach ($list as $key=>$value) {
            $cardLogo = CardLogo::where('bank_code', $value['bank_code'])->first();
            $data[$key] = $value;
            $data[$key]['card_logo_url'] = !empty($cardLogo) ? getOssImageUrl($cardLogo->card_logo_url) : '';
            $creditCardHandleGrant = CreditCardHandleGrant::find($value['grant_type_id']);
            $data[$key]['grant_type'] = !empty($creditCardHandleGrant) ? $creditCardHandleGrant->grant_type : '';
            unset($data[$key]['bank_code']);
            unset($data[$key]['grant_type_id']);
        }
        return $data;
    }

    public function findByOpen($id)
    {
        return CreditCardHandle::where(['id' => $id, 'is_open' => 1])->first();
    }

}